package mc.support.taobao.api;

import mc.support.taobao.api.fields.GetItemsListFields;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.request.ItemsListGetRequest;
import com.taobao.api.response.ItemsListGetResponse;
import org.apache.commons.lang.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA. <br/>
 * Author: francis Yun    <br/>
 * Date: 2013-11-22  <br/>
 */
public class GetItemsListReq extends AbstractReq<GetItemsListReq, ItemsListGetResponse> {

    private Set<String> numIids;
    private Set<String> trackIids;
    private GetItemsListFields fields;

    public GetItemsListReq fields(GetItemsListFields fields) {
        this.fields = fields;
        return this;
    }

    public GetItemsListReq addNumIid(String numIid) {
        if ( null == this.numIids ) {
            this.numIids = new HashSet<String>();
        }
        this.numIids.add( numIid );
        return this;
    }

    public GetItemsListReq removeNumIid(String numIid) {
        if ( null != this.numIids ) {
            this.numIids.remove(numIid);
        }
        return this;
    }

    public GetItemsListReq addNumIids(Set<String> numIids) {
        if ( null == this.numIids ) {
            this.numIids = numIids;
        } else {
            this.numIids.addAll( numIids );
        }
        return this;
    }

    public GetItemsListReq addNumIids(String[] numIids) {
        if ( null != numIids && numIids.length > 0 ) {
            for ( int i = 0; i < numIids.length; i++ ) {
                addNumIid(numIids[i]);
            }
        }
        return this;
    }

    public GetItemsListReq addTrackIid(String trackIid) {
        if ( null == trackIids ) {
            trackIids = new HashSet<String>();
        }
        trackIids.add( trackIid );
        return this;
    }

    public GetItemsListReq addTrackIids(Set<String> trackIids) {
        if ( null == trackIids ) {
            this.trackIids = trackIids;
        } else {
            this.trackIids.addAll(trackIids);
        }
        return this;
    }

    public GetItemsListReq addTrackIids(String[] trackIids) {
        if ( null != trackIids && trackIids.length > 0 ) {
            for ( int i = 0; i < trackIids.length; i++ ) {
                addTrackIid( trackIids[i] );
            }
        }
        return this;
    }

    public GetItemsListReq removeTrackIid(String trackIid) {
        if ( null != trackIids ) {
            trackIids.remove( trackIids );
        }
        return this;
    }

    public GetItemsListReq numIids(Set<String> numIids) {
        this.numIids = numIids;
        return this;
    }

    public GetItemsListReq trackIids(Set<String> trackIids) {
        this.trackIids = trackIids;
        return this;
    }

    public Set<String> getTrackIids() {
        return trackIids;
    }

    public Set<String> getNumIids() {
        return numIids;
    }

    @Override
    protected TaobaoRequest toRequest() {
        ItemsListGetRequest req = new ItemsListGetRequest();

        if ( null != fields ) {
            req.setFields( fields.toString() );
        }

        if ( null != numIids ) {
            String numIdsStr = StringUtils.join( numIids, ',' );
            req.setNumIids( numIdsStr );
        }
        if ( null != trackIids ) {
            String trackIidsStr = StringUtils.join( trackIids, ',' );
            req.setTrackIids( trackIidsStr );
        }

        return req;
    }

}
